﻿using ZYCommon;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace TextEditor
{
    

    public class ZYTextObject : ZYTextElement
    {
        protected bool bolCanResize = true;
        protected double dblWidthHeightRate = 0.0;

        public override bool HandleClick(int x, int y, MouseButtons Button)
        {
            if (!base.myOwnerDocument.OwnerControl.myPopupMenu.Visible)
            {
                if (this.Bounds.Contains(x, y) && (base.myOwnerDocument.Content.SelectLength == 0))
                {
                    base.myOwnerDocument.Content.SetSelection(base.myOwnerDocument.IndexOf(this), 1);
                    base.myOwnerDocument.RefreshElement(this);
                    base.myOwnerDocument.HideCaret();
                    return true;
                }
            }
            return false;
        }

        public override bool HandleMouseDown(int x, int y, MouseButtons Button)
        {
            if (this.ShowDragRect())
            {
                Rectangle[] rectangleArray = DocumentView.GetDragRects(new Rectangle(this.RealLeft, this.RealTop, this.Width - 1, this.Height - 1), 18, true);
                for (int i = 0; i < rectangleArray.Length; i++)
                {
                    if (rectangleArray[i].Contains(x, y))
                    {
                        if (base.myOwnerDocument.OwnerControl != null)
                        {
                            Rectangle rectangle = base.myOwnerDocument.OwnerControl.CaptureDragRect(this.Bounds, i, true, this.dblWidthHeightRate, true, null,this.RealTop);
                            if ((rectangle.Width > 10) && (rectangle.Height > 10))
                            {
                                base.myOwnerDocument.BeginContentChangeLog();
                                this.Width = rectangle.Width;
                                this.Height = rectangle.Height;
                                base.myOwnerDocument.EndContentChangeLog();
                                if (this.Parent is ZYTextTableCell)
                                {
                                    this.Parent.Parent.Parent.RefreshSize();
                                }
                                base.myOwnerDocument.RefreshLine();
                                base.myOwnerDocument.UpdateView();
                                base.myOwnerDocument.Modified = true;
                                return true;
                            }
                        }
                        return true;
                    }
                }
            }
            return false;
        }

        public override bool HandleMouseMove(int x, int y, MouseButtons Button)
        {
            if (this.ShowDragRect())
            {
                Rectangle[] rectangleArray = DocumentView.GetDragRects(new Rectangle(this.myBounds.Left, this.myBounds.Top, this.myBounds.Width - 1, this.myBounds.Height - 1), 6, true);
                for (int i = 0; i < rectangleArray.Length; i++)
                {
                    if (rectangleArray[i].Contains(x, y))
                    {
                        Cursor dragRectCursor = DocumentView.GetDragRectCursor(i);
                        if (dragRectCursor != null)
                        {
                            base.myOwnerDocument.SetCursor(dragRectCursor);
                        }
                        return true;
                    }
                }
            }
            if (this.Bounds.Contains(x, y))
            {
                base.myOwnerDocument.SetCursor(Cursors.Default);
            }
            return false;
        }

        public override bool RefreshView()
        {
            if (base.myOwnerDocument.isSelected(this))
            {
                if (this.ShowDragRect())
                {
                    base.myOwnerDocument.View.DrawRectangle(Color.Black, new Rectangle(this.myBounds.Left, this.myBounds.Top, this.myBounds.Width - 1, this.myBounds.Height - 1));
                    base.myOwnerDocument.View.DrawDragRect(new Rectangle(this.myBounds.Left, this.myBounds.Top, this.myBounds.Width - 1, this.myBounds.Height - 1), 6, true, Color.Black, Color.White);
                }
                else if (!this.Deleteted && (base.myOwnerDocument.Content.CurrentSelectElement == this))
                {
                    base.myOwnerDocument.View.DrawRectangle(Color.Black, new Rectangle(this.myBounds.Left, this.myBounds.Top, this.myBounds.Width - 1, this.myBounds.Height - 1));
                }
            }
            return true;
        }

        protected bool ShowDragRect()
        {
            return (((this.bolCanResize && !base.myParent.Locked) && !this.Deleteted) && (base.myOwnerDocument.Content.CurrentSelectElement == this));
        }

        public override void UpdateAttrubute()
        {
            base.Width = StringCommon.ToInt32Value(base.myAttributes.GetString("width"), base.Width);
            base.Height = StringCommon.ToInt32Value(base.myAttributes.GetString("height"), base.Height);
            base.UpdateAttrubute();
        }

        public bool CanResize
        {
            get
            {
                return this.bolCanResize;
            }
            set
            {
                this.bolCanResize = value;
            }
        }

        public override int Height
        {
            get
            {
                return base.intHeight;
            }
            set
            {
                base.intHeight = value;
                base.myAttributes.SetValue("height", value.ToString());
            }
        }

        public string ID
        {
            get
            {
                return base.myAttributes.GetString("id");
            }
            set
            {
                base.myAttributes.SetValue("id", value);
            }
        }

        public override int Width
        {
            get
            {
                return base.intWidth;
            }
            set
            {
                base.intWidth = value;
                base.myAttributes.SetValue("width", value.ToString());
            }
        }

        public double WidthHeightRate
        {
            get
            {
                return this.dblWidthHeightRate;
            }
            set
            {
                this.dblWidthHeightRate = value;
            }
        }
    }
}

